package com.pri.chat.base;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import com.bumptech.glide.Glide;
import com.netease.nim.uikit.net.StatusBarUtil;
import com.netease.nim.uikit.net.progress.ProgressDialogHandler;
import com.pri.chat.R;
import com.pri.chat.application.APP;
import com.pri.chat.application.ActivityBean;
import com.netease.nim.uikit.net.SharedHelper;
import com.vondear.rxtool.view.RxToast;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by G on 2018/3/16.`
 */

public abstract class BaseActivity extends AppCompatActivity {

    protected String TAG = this.getClass().getSimpleName();

    public RelativeLayout rl_center;
    public TextView tv_title;
    public ImageView back;
    public TextView btnRight;
    public ImageView iv_paizhao;
    public Activity activity;
    public Context mContext;
//    public boolean isFinish = true;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutId());
        activity = this;
        mContext = this;
        APP.getInstance().add(this);
        rl_center = findViewById(R.id.rl_center);
        StatusBarUtil.setColor(this, getResources().getColor(R.color.white), 0);
        StatusBarUtil.setTranslucentForImageViewInFragment(this, 0, rl_center);
        StatusBarUtil.setLightMode(this);

        ButterKnife.bind(this);

        bindView();
        initTitle();
        initView();
        initData();
    }

    @Override
    protected void onDestroy() {
        ActivityBean bean = getIntent().getParcelableExtra("ActivityBean");
        bean.getUnbinder().unbind();
        super.onDestroy();
    }

    private ProgressDialogHandler mProgressDialogHandler;

    public void showProgressDialog() {
        if (mProgressDialogHandler != null) {
            mProgressDialogHandler.obtainMessage(ProgressDialogHandler.SHOW_PROGRESS_DIALOG).sendToTarget();
        }
    }

    public void dismissProgressDialog() {
        if (mProgressDialogHandler != null) {
            mProgressDialogHandler.obtainMessage(ProgressDialogHandler.DISMISS_PROGRESS_DIALOG).sendToTarget();
            mProgressDialogHandler = null;
        }
    }

    /**
     * 加载布局
     *
     * @return
     */
    protected abstract int getLayoutId();

    private void bindView() {
        ActivityBean bean = new ActivityBean();
        Unbinder unbinder = ButterKnife.bind(this);
        bean.setUnbinder(unbinder);
        getIntent().putExtra("ActivityBean", bean);
    }

    public void showToams(String s) {
        RxToast.normal(s);
    }

    protected void initTitle() {
        back = findViewById(R.id.iv_back);
        tv_title = findViewById(R.id.title);
        btnRight = findViewById(R.id.tv_release);
        iv_paizhao = findViewById(R.id.iv_paizhao);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    public void setTitle(String title) {
        tv_title.setText(title);
    }


    /**
     * 添加控件
     */
    protected abstract void initView();

    /**
     * 添加数据
     */
    protected abstract void initData();

    /**
     * startActivity
     */
    protected void readyGo(Context context, Class<?> clazz) {
        Intent intent = new Intent(context, clazz);
        context.startActivity(intent);
    }

    private void changeBar() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            //实现状态栏图标和文字颜色为暗色
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }
        getWindow().getDecorView().findViewById(android.R.id.content).setPadding(0, 0, 0, 22);
    }

    /**
     * 在有输入文本打开输入法的时候，点击其他空白区域收起输入法
     *
     * @param event
     * @return
     */
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (null != this.getCurrentFocus()) {
            InputMethodManager mInputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
            return mInputMethodManager.hideSoftInputFromWindow(this
                    .getCurrentFocus().getWindowToken(), 0);
        }
        return super.onTouchEvent(event);
    }

    /**
     * 当点击toolbar上的返回箭头，退出当前activity
     *
     * @param item
     * @return
     */
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
//                if(isFinish){
                finish();
//                }
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    public boolean isNull(EditText editText) {
        if (editText.getText().toString() == null | editText.getText().toString().equals("")) {
            return true;
        } else {
            return false;
        }
    }

    public boolean isNull(TextView editText) {
        if (editText.getText().toString().isEmpty()) {
            return true;
        } else {
            return false;
        }
    }

    public void setImg(ImageView imageView, Object object) {
        Glide.with(this).load(object).into(imageView);
    }

    public boolean isNull(String editText) {
        if (editText == null || editText.equals("")) {
            return true;
        } else {
            return false;
        }
    }

    public String getString(EditText editText) {
        return editText.getText().toString();
    }

    public String getString(TextView editText) {
        return editText.getText().toString();
    }

    public int getUid() {
        return Integer.parseInt(SharedHelper.readId(this));
    }
}
